package com.wan.util.number;

import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Consumer;

public class LongRange {

    private final long start;

    private final long end;

    private LongRange(long start,long end){
        this.start = start;
        this.end = end;
    }

    public static LongRange of(long start,long end){
        return new LongRange(start,end);
    }

    public long getStart() {
        return start;
    }

    public long getEnd() {
        return end;
    }

    /**
     * start->end 包含开始和结束
     */
    public void foreach(Consumer<Long> consumer){
        for (long i = start; i <= end; i++) {
            consumer.accept(i);
        }
    }

    /**
     * 不包含结束
     */
    public void foreachExcludeEnd(Consumer<Long> consumer){
        for (long i = start; i < end; i++) {
            consumer.accept(i);
        }
    }

    /**
     * 不包含开始
     */
    public void foreachExcludeBegin(Consumer<Long> consumer){
        for (long i = start + 1; i <= end; i++) {
            consumer.accept(i);
        }
    }

    /**
     * 范围内随机一个，包含开始和结束
     */
    public long randomNext(){
        return ThreadLocalRandom.current().nextLong(start,end + 1);
    }

}
